/* 
 *   ConsoleInput: Console Input Library For Java 
 *   Copyright (C) 2002  Yohanes Nugroho 
 * 
 *   This program is free software; you can redistribute it and/or 
modify 
 *   it under the terms of the GNU General Public License as published 
by 
 *   the Free Software Foundation; either version 2 of the License, or 
 *   (at your option) any later version. 
 * 
 *   This program is distributed in the hope that it will be useful, 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *   GNU General Public License for more details. 
 * 
 *   You should have received a copy of the GNU General Public License 
 *   along with this program; if not, write to the Free Software 
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
1307  USA 
 */ 

package ConsoleInputLib;
 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 

/** 
 * A Class for reading input from console 
 * @author Yohanes Nugroho 
 * @version 0.5, 03/19/2002 
 */ 
public class ConsoleInput { 
 
 private static BufferedReader br; 
 
 /** 
  * this class can not be instantiated 
  */ 
 private ConsoleInput(){ 
 
 } 
  
 /** 
  * private method for creating the BufferedReader object 
  */ 
 private static void init(){ 
                br = new BufferedReader(new InputStreamReader 
                        (System.in)); 
 } 
  
 /** 
  * reads a line of string (terminated by '\n') from 
  * the console 
  * @return line read 
  */ 
 public static String readString(){ 
  if (br==null) init(); 
  String tmp; 
                try { 
                        tmp = br.readLine(); 
                } catch (IOException e){ 
   tmp = null; 
                } 
  return tmp; 
 } 
 
 /** 
  * converts a string to an integer 
  * @param s string to be converted 
  * @return the converted value, or 0 if it fails 
  * @see #isValidInt 
  */ 
 public static int strToInt(String s){ 
  if (s==null) return 0; 
  int tmp; 
  try { 
   tmp = Integer.parseInt(s); 
  } catch (NumberFormatException e){ 
   tmp = 0; 
  } 
  return tmp; 
 } 
  /** 
  * checks wether a string can be converted to an integer 
  * @param s string to be checked 
  * @return true if s can be converted, false otherwise 
  */ 
 public static boolean isValidInt(String s){ 
  try { 
   Integer.parseInt(s); 
  } catch (NumberFormatException e){ 
   return false; 
  } 
  return true; 
 } 
  
 /** 
  * reads an integer from the console 
  * @return the integer value read from the console  
         * (or 0 if the supplied value cannot be converted to an 
integer) 
         */ 
 public static int readInt(){ 
  return strToInt(readString()); 
 } 
 
 /** 
  * reads an integer from the console 
  * @param defaultvalue default value returned if the input from 
the 
  *  console is not a valid integer 
  * @return the integer value read from the console  
         * (or <code>defaultvalue</code> if the supplied value cannot 
be converted  
         * to an integer) 
         */ 
 public static int readInt(int defaultvalue){ 
  String s = readString(); 
  if (isValidInt(s)) return strToInt(s); 
  else return defaultvalue; 
 } 
 
 /** 
  * converts a string to a double 
  * @param s string to be converted 
  * @return the converted value, or 0 if it fails 
  * @see #isValidDouble 
  */ 
 public static double strToDouble(String s){ 
  if (s==null) return 0; 
  double tmp; 
  try { 
   tmp = Double.parseDouble(s); 
  } catch (NumberFormatException e){ 
   tmp = 0; 
  } 
  return tmp; 
 } 
  /** 
  * checks wether a string can be converted to a double 
  * @param s string to be checked 
  * @return true if s can be converted, false otherwise 
  */ 
 public static boolean isValidDouble(String s){ 
  try { 
   Double.parseDouble(s); 
  } catch (NumberFormatException e){ 
   return false; 
  } 
  return true; 
 } 
 
 /** 
  * reads an double from the console 
  * @return the double value read from the console  
  * (or 0 if the supplied value cannot be converted to a double) 
         */    
 public static double readDouble(){ 
  return strToDouble(readString()); 
 } 
 
 /** 
  * reads an double from the console 
  * @param defaultvalue default value returned if the input from 
the 
  * console is not a valid double 
  * @return the double value read from the console  
         * (or <code>defaultvalue</code> if the supplied value cannot 
be converted  
         * to a double) 
         */ 
 public static double readDouble(double defaultvalue){ 
  String s = readString(); 
  if (isValidDouble(s)) return strToDouble(s); 
  else return defaultvalue; 
 } 
 
} 